// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.metadata.types;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class DenguesTypesManager {

    public static final JavaType BOOLEAN = new JavaType(Boolean.class, boolean.class);

    public static final JavaType BYTE = new JavaType(Byte.class, byte.class);

    public static final JavaType BYTE_ARRAY = new JavaType(byte[].class);

    public static final JavaType CHARACTER = new JavaType(Character.class, char.class);

    public static final JavaType DATE = new JavaType(Date.class, true);

    public static final JavaType DOUBLE = new JavaType(Double.class, double.class);

    public static final JavaType FLOAT = new JavaType(Float.class, float.class);

    public static final JavaType INTEGER = new JavaType(Integer.class, int.class);

    public static final JavaType LONG = new JavaType(Long.class, long.class);

    public static final JavaType SHORT = new JavaType(Short.class, short.class);

    public static final JavaType STRING = new JavaType(String.class);

    public static final JavaType OBJECT = new JavaType(Object.class);

    public static final JavaType LIST = new JavaType(List.class);

    public static final JavaType BIGDECIMAL = new JavaType(BigDecimal.class);

    public static final JavaType[] JAVA_TYPES = new JavaType[] { BOOLEAN, BYTE, BYTE_ARRAY, CHARACTER, DATE, DOUBLE, FLOAT,
            INTEGER, LONG, OBJECT, SHORT, STRING, LIST, BIGDECIMAL };

    public static final String DEFAULT_CHAR = "' '"; //$NON-NLS-1$

    public static final String DEFAULT_BOOLEAN = "false"; //$NON-NLS-1$

    public static final String DEFAULT_NUMBER = "0"; //$NON-NLS-1$

    public static final String DEFAULT_FLOAT = "0.0f";

    public static final String DEFAULT_DOUBLE = "0.0";

    public static final String NULL = "null"; //$NON-NLS-1$

    public static final JavaType[] NUMBER_TYPES = new JavaType[] { INTEGER, FLOAT, DOUBLE, LONG, SHORT, BIGDECIMAL, BYTE };

    public static JavaType getJavaTypeFromDBType(String typeId) {
        for (JavaType type : JAVA_TYPES) {
            if (type.getId().equals(typeId)) {
                return type;
            }
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getJavaTypeFromTypeName".
     * 
     * @param typeName
     * @return
     */
    public static String getJavaTypeFromTypeName(String typeName) {
        JavaType javaType = null;
        for (JavaType type : JAVA_TYPES) {
            Class nullableClass = type.getNullableClass();
            try {
                Class<?> forName = Class.forName(typeName);
                if (forName == nullableClass || forName == type.getPrimitiveClass()) {
                    javaType = type;
                    break;
                }
            } catch (ClassNotFoundException e) {
                if (!typeName.contains("java.lang.")) {
                    return getJavaTypeFromTypeName("java.lang." + typeName);
                }
            }
        }
        return getDefault(javaType);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDefault".
     * 
     * @param javaType
     * @return
     */
    public static String getDefault(JavaType javaType) {
        if (javaType == null) {
            return NULL;
        } else if (javaType.equals(BOOLEAN)) {
            return DEFAULT_BOOLEAN;
        } else if (javaType.equals(LONG) || javaType.equals(SHORT) || javaType.equals(INTEGER) || javaType.equals(BYTE)) {
            return DEFAULT_NUMBER;
        } else if (javaType.equals(DOUBLE)) {
            return DEFAULT_DOUBLE;
        } else if (javaType.equals(FLOAT)) {
            return DEFAULT_FLOAT;
        } else if (javaType.equals(CHARACTER)) {
            return DEFAULT_CHAR;
        }
        return NULL;
    }
}
